package lc.链表;

import utils.ListNode;

import java.util.Stack;

/**
 * @description:
 * @author: qian
 * @createDate: 2021/10/13
 */
public class 从尾到头打印链表 {

    public int[] reversePrint(ListNode head) {


        Stack<Integer> stack = new Stack<>();
        ListNode root = head;
        while (root != null) {
            stack.push(root.val);
            root = root.next;
        }
        int[] arr = new int[stack.size()];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = stack.pop();
        }
        return arr;
    }

    public int[] reversePrint1(ListNode head) {

        int count = 0;
        ListNode root = head;
        while (root != null) {
            count++;
            root = root.next;
        }

        int[] arr = new int[count];
        root = head;
        for (int i = arr.length - 1; i >= 0; i--) {
            arr[i] = root.val;
            root = root.next;
        }
        return arr;
    }
}
